<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form slot="detail" :form="form">
        <a-row>
          <a-col :span="12">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="所属中心">
              <j-dict-select-tag v-decorator="['auditGrade',validatorRules.auditGrade]" :trigger-change="true"
                                 dictCode="audit_grade" placeholder="请选择所属中心" type="list"
                                 :disabled='model.bpmStatus==="3"'/>
            </a-form-item>
          </a-col>
          <a-col :span='12'>
            <a-form-item :labelCol='labelCol' :wrapperCol='wrapperCol' label='项目简称'>
              <j-popup
                v-decorator="['projectName',validatorRules.projectName]"
                :trigger-change='true'
                code='pm_project_select'
                dest-fields='projectId,projectName'
                org-fields='id,short_name'
                :disabled='model.bpmStatus==="3"'
                @callback='popupCallback'
              />
              <input v-show='false' v-decorator="['projectId']"></input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="借款日期">
              <j-date v-decorator="['borrowDate',validatorRules.borrowDate]" :trigger-change="true"
                      placeholder="请选择借款日期" style="width: 100%"
                      :disabled='model.bpmStatus==="3"'/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="借款金额">
              <a-input-number v-decorator="['money',validatorRules.money]" placeholder="请输入借款金额" style="width: 100%"
                              :disabled='model.bpmStatus==="3"'/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="借款周期">
              <a-input v-decorator="['borrowPeriod',validatorRules.borrowPeriod]" placeholder="请输入借款周期"
                       :disabled='model.bpmStatus==="3"'></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item :labelCol="labelCol2" :wrapperCol="wrapperCol2" label="借款缘由">
              <a-textarea v-decorator="['remake',validatorRules.remake]" placeholder="请输入借款缘由" rows="4"
                          :disabled='model.bpmStatus==="3"' />
            </a-form-item>
          </a-col>
          <a-col :span="12" v-if='model.bpmStatus==="3"'>
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="确认人">
              <a-input v-decorator="['confirmPerson',validatorRules.confirmPerson]"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12"  v-if='model.bpmStatus==="3"'>
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="归还日期">
              <j-date v-decorator="['returnDate',validatorRules.returnDate]" :trigger-change="true"
                      placeholder="请选择借款日期" style="width: 100%"
                     />
            </a-form-item>
          </a-col>
          <a-col :span="24"  v-if='model.bpmStatus==="3"'>
            <a-form-item :labelCol="labelCol2" :wrapperCol="wrapperCol2" label="备注">
              <a-textarea v-decorator="['details',validatorRules.details]" placeholder="请输入借款缘由" rows="4"
                          />
            </a-form-item>
          </a-col>
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
  </a-spin>
</template>

<script>

import {getAction, httpAction} from '@/api/manage'
import pick from 'lodash.pick'

export default {
  name: 'HfPmPettyCashForm',
  components: {},
  props: {
    //流程表单data
    formData: {
      type: Object,
      default: () => {
      },
      required: false
    },
    //表单模式：true流程表单 false普通表单
    formBpm: {
      type: Boolean,
      default: false,
      required: false
    },
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  watch: {
    '$store.state.bpm.bpmSubmitStates': {
      immediate: true,
      handler(val) {
        if (val && val.edit && val.edit === '1') {
          this.submitForm().then(res => {
            if (res) {
              this.$store.commit('BPM_SUBMIT_STATES', {edit: '2'})
            }
          })
        }
      },
    }
  },
  data() {
    return {
      form: this.$form.createForm(this),
      model: {},
      labelCol: {
        xs: {span: 24},
        sm: {span: 6},
      },
      wrapperCol: {
        xs: {span: 24},
        sm: {span: 16},
      },
      labelCol2: {
        xs: {span: 24},
        sm: {span: 3},
      },
      wrapperCol2: {
        xs: {span: 24},
        sm: {span: 20},
      },
      confirmLoading: false,
      validatorRules: {
        confirmPerson: {
          rules: [
            {required: true, message: '请输入确认人!'}
          ]
        },
        details: {
          rules: [
            {required: true, message: '请输入备注!'}
          ]
        },
        returnDate: {
          rules: [
            {required: true, message: '请选择归还日期!'}
          ]
        },
        auditGrade: {
          rules: [
            {required: true, message: '请选择所属中心!'}
          ]
        },
        projectName: {
          rules: [
            {required: true, message: '请选择项目!'}
          ]
        },
        borrowDate: {
          rules: [
            {required: true, message: '请选择日期!'}
          ]
        },
        borrowPeriod: {
          rules: [
            {required: true, message: '请输入借款周期!'}
          ]
        },
        money: {
          rules: [
            {required: true, message: '请输入借款金额!'}
          ]
        },
        remake: {
          rules: [
            {required: true, message: '请选择借款缘由!'}
          ]
        },
      },
      url: {
        add: "/pm/hfPmPettyCash/add",
        edit: "/pm/hfPmPettyCash/edit",
        queryById: "/pm/hfPmPettyCash/queryById"
      },
      fields: [
        'createBy', 'createTime', 'auditGrade', 'borrowDate',
        'money', 'borrowPeriod', 'remake', 'isReturn', 'bpmStatus', 'projectName', 'projectId','returnDate','details','confirmPerson'
      ]

    }
  },
  computed: {
    formDisabled() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return false
        }
        return true
      }
      return this.disabled
    },
    showFlowSubmitButton() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return true
        }
      }
      return false
    }
  },
  created() {
    //如果是流程中表单，则需要加载流程表单data
    this.showFlowData();
  },
  methods: {
    add() {
      this.edit({});
    },
    edit(record) {
      this.form.resetFields();
      this.model = Object.assign({}, record);
      this.visible = true;
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.model, this.fields))
      })
    },
    //渲染流程表单数据
    showFlowData() {
      if (this.formBpm === true) {
        let params = {id: this.formData.dataId};
        getAction(this.url.queryById, params).then((res) => {
          if (res.success) {
            this.edit(res.result);
          }
        });
      }
    },
    submitForm() {
      const that = this
      return new Promise(((resolve, reject) => {
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true
            let httpurl = ''
            let method = ''
            if (!this.model.id) {
              httpurl += this.url.add
              method = 'post'
            } else {
              httpurl += this.url.edit
              method = 'put'
            }
            let formData = Object.assign(this.model, values)
            console.log('表单提交数据', formData)
            httpAction(httpurl, formData, method).then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
                resolve(true)
              } else {
                that.$message.warning(res.message)
              }
            }).finally(() => {
              that.confirmLoading = false
            })
          } else {
            this.$store.commit('BPM_SUBMIT_STATES', {})
          }
        })
      }))
    },
    popupCallback(row) {
      this.form.setFieldsValue(pick(row, this.fields))
    },
  }
}
</script>